//
//  AddNewsPictureView.m
//  Findif
//
//  Created by Mac on 2019/1/23.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "AddNewsPictureView.h"

@interface AddNewsPictureView ()

@property (nonatomic, strong)UIButton * oneButton;
@property (nonatomic, strong)UIButton * twoButton;
@property (nonatomic, strong)UIButton * threeButton;
@property (nonatomic, strong)UIButton * fourButton;
@property (nonatomic, strong)UIButton * fiveButton;
@property (nonatomic, strong)UIButton * sixButton;
@property (nonatomic, strong)UIButton * sevenButton;
@property (nonatomic, strong)UIButton * eightButton;
@property (nonatomic, strong)UIButton * nineButton;
@property (nonatomic, strong)UIButton * oneDeleteButton;
@property (nonatomic, strong)UIButton * twoDeleteButton;
@property (nonatomic, strong)UIButton * threeDeleteButton;
@property (nonatomic, strong)UIButton * fourDeleteButton;
@property (nonatomic, strong)UIButton * fiveDeleteButton;
@property (nonatomic, strong)UIButton * sixDeleteButton;
@property (nonatomic, strong)UIButton * sevenDeleteButton;
@property (nonatomic, strong)UIButton * eightDeleteButton;
@property (nonatomic, strong)UIButton * nineDeleteButton;

@property (nonatomic, strong)NSMutableArray * buttonArray;
@property (nonatomic, strong)NSMutableArray * deleteArray;

@end

@implementation AddNewsPictureView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        self.buttonArray = [NSMutableArray array];
        self.deleteArray = [NSMutableArray array];
        [self setupUI];
    }
    
    return self;
}


- (void)setCount:(NSInteger )count{
    
    _count = count;
    
    switch (count) {
        case 1:
            self.twoButton.hidden = NO;
            self.threeButton.hidden = YES;
            self.fourButton.hidden = YES;
            self.fiveButton.hidden = YES;
            self.sixButton.hidden = YES;
            self.sevenButton.hidden = YES;
            self.eightButton.hidden = YES;
            self.nineButton.hidden = YES;
            break;
        case 2:
            self.threeButton.hidden = NO;
            self.fourButton.hidden = YES;
            self.fiveButton.hidden = YES;
            self.sixButton.hidden = YES;
            self.sevenButton.hidden = YES;
            self.eightButton.hidden = YES;
            self.nineButton.hidden = YES;
            break;
        case 3:
            self.fourButton.hidden = NO;
            self.fiveButton.hidden = YES;
            self.sixButton.hidden = YES;
            self.sevenButton.hidden = YES;
            self.eightButton.hidden = YES;
            self.nineButton.hidden = YES;
            break;
        case 4:
            self.fiveButton.hidden = NO;
            self.sixButton.hidden = YES;
            self.sevenButton.hidden = YES;
            self.eightButton.hidden = YES;
            self.nineButton.hidden = YES;
            break;
        case 5:
            self.sixButton.hidden = NO;
            self.sevenButton.hidden = YES;
            self.eightButton.hidden = YES;
            self.nineButton.hidden = YES;
            break;
        case 6:
            self.sevenButton.hidden = NO;
            self.eightButton.hidden = YES;
            self.nineButton.hidden = YES;
            break;
        case 7:
            self.eightButton.hidden = NO;
            self.nineButton.hidden = YES;
            break;
        case 8:
            self.nineButton.hidden = NO;
            break;
        default:
            break;
    }
}


- (void)setButtonImage:(UIImage *)buttonImage{
    
    _buttonImage = buttonImage;
    
    switch (self.count) {
        case 1:
            [self.oneButton setImage:buttonImage forState:UIControlStateNormal];
            self.oneDeleteButton.hidden = NO;
            break;
        case 2:
            [self.twoButton setImage:buttonImage forState:UIControlStateNormal];
            self.twoDeleteButton.hidden = NO;
            break;
        case 3:
            [self.threeButton setImage:buttonImage forState:UIControlStateNormal];
            self.threeDeleteButton.hidden = NO;
            break;
        case 4:
            [self.fourButton setImage:buttonImage forState:UIControlStateNormal];
            self.fourDeleteButton.hidden = NO;
            break;
        case 5:
            [self.fiveButton setImage:buttonImage forState:UIControlStateNormal];
            self.fiveDeleteButton.hidden = NO;
            break;
        case 6:
            [self.sixButton setImage:buttonImage forState:UIControlStateNormal];
            self.sixDeleteButton.hidden = NO;
            break;
        case 7:
            [self.sevenButton setImage:buttonImage forState:UIControlStateNormal];
            self.sevenDeleteButton.hidden = NO;
            break;
        case 8:
            [self.eightButton setImage:buttonImage forState:UIControlStateNormal];
            self.eightDeleteButton.hidden = NO;
            break;
        case 9:
            [self.nineButton setImage:buttonImage forState:UIControlStateNormal];
            self.nineDeleteButton.hidden = NO;
            break;
        default:
            break;
    }
}


- (void)setUrl:(NSURL *)url{
    
    _url = url;
    
    switch (self.count) {
        case 1:
            [self.oneButton sd_setImageWithURL:url forState:UIControlStateNormal];
            self.oneDeleteButton.hidden = NO;
            break;
        case 2:
            [self.twoButton sd_setImageWithURL:url forState:UIControlStateNormal];
            self.twoDeleteButton.hidden = NO;
            break;
        case 3:
            [self.threeButton sd_setImageWithURL:url forState:UIControlStateNormal];
            self.threeDeleteButton.hidden = NO;
            break;
        case 4:
            [self.fourButton sd_setImageWithURL:url forState:UIControlStateNormal];
            self.fourDeleteButton.hidden = NO;
            break;
        case 5:
            [self.fiveButton sd_setImageWithURL:url forState:UIControlStateNormal];
            self.fiveDeleteButton.hidden = NO;
            break;
        case 6:
            [self.sixButton sd_setImageWithURL:url forState:UIControlStateNormal];
            self.sixDeleteButton.hidden = NO;
            break;
        case 7:
            [self.sevenButton sd_setImageWithURL:url forState:UIControlStateNormal];
            self.sevenDeleteButton.hidden = NO;
            break;
        case 8:
            [self.eightButton sd_setImageWithURL:url forState:UIControlStateNormal];
            self.eightDeleteButton.hidden = NO;
            break;
        case 9:
            [self.nineButton sd_setImageWithURL:url forState:UIControlStateNormal];
            self.nineDeleteButton.hidden = NO;
            break;
        default:
            break;
    }
}


- (void)setupUI{
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.oneButton = [self setupButton];
    self.oneButton.tag = 1;
    [self addSubview:self.oneButton];
    self.twoButton = [self setupButton];
    self.twoButton.tag = 2;
    [self addSubview:self.twoButton];
    self.threeButton = [self setupButton];
    self.threeButton.tag = 3;
    [self addSubview:self.threeButton];
    self.fourButton = [self setupButton];
    self.fourButton.tag = 4;
    [self addSubview:self.fourButton];
    self.fiveButton = [self setupButton];
    self.fiveButton.tag = 5;
    [self addSubview:self.fiveButton];
    self.sixButton = [self setupButton];
    self.sixButton.tag = 6;
    [self addSubview:self.sixButton];
    self.sevenButton = [self setupButton];
    self.sevenButton.tag = 7;
    [self addSubview:self.sevenButton];
    self.eightButton = [self setupButton];
    self.eightButton.tag = 8;
    [self addSubview:self.eightButton];
    self.nineButton = [self setupButton];
    self.nineButton.tag = 9;
    [self addSubview:self.nineButton];
    
    [self.buttonArray addObject:self.oneButton];
    [self.buttonArray addObject:self.twoButton];
    [self.buttonArray addObject:self.threeButton];
    [self.buttonArray addObject:self.fourButton];
    [self.buttonArray addObject:self.fiveButton];
    [self.buttonArray addObject:self.sixButton];
    [self.buttonArray addObject:self.sevenButton];
    [self.buttonArray addObject:self.eightButton];
    [self.buttonArray addObject:self.nineButton];
    
    self.oneButton.hidden = NO;
    
    self.oneDeleteButton = [self setupDeleteButton];
    self.oneDeleteButton.tag = 1;
    [self.oneButton addSubview:self.oneDeleteButton];
    self.twoDeleteButton = [self setupDeleteButton];
    self.twoDeleteButton.tag = 2;
    [self.twoButton addSubview:self.twoDeleteButton];
    self.threeDeleteButton = [self setupDeleteButton];
    self.threeDeleteButton.tag = 3;
    [self.threeButton addSubview:self.threeDeleteButton];
    self.fourDeleteButton = [self setupDeleteButton];
    self.fourDeleteButton.tag = 4;
    [self.fourButton addSubview:self.fourDeleteButton];
    self.fiveDeleteButton = [self setupDeleteButton];
    self.fiveDeleteButton.tag = 5;
    [self.fiveButton addSubview:self.fiveDeleteButton];
    self.sixDeleteButton = [self setupDeleteButton];
    self.sixDeleteButton.tag = 6;
    [self.sixButton addSubview:self.sixDeleteButton];
    self.sevenDeleteButton = [self setupDeleteButton];
    self.sevenDeleteButton.tag = 7;
    [self.sevenButton addSubview:self.sevenDeleteButton];
    self.eightDeleteButton = [self setupDeleteButton];
    self.eightDeleteButton.tag = 8;
    [self.eightButton addSubview:self.eightDeleteButton];
    self.nineDeleteButton = [self setupDeleteButton];
    self.nineDeleteButton.tag = 9;
    [self.nineButton addSubview:self.nineDeleteButton];
    
    [self.deleteArray addObject:self.oneDeleteButton];
    [self.deleteArray addObject:self.twoDeleteButton];
    [self.deleteArray addObject:self.threeDeleteButton];
    [self.deleteArray addObject:self.fourButton];
    [self.deleteArray addObject:self.sixButton];
    [self.deleteArray addObject:self.sevenButton];
    [self.deleteArray addObject:self.eightButton];
    [self.deleteArray addObject:self.nineButton];
}

-(void)layoutSubviews{
    
    [self.oneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(15 * SCREENH6);
        make.top.mas_equalTo(self).offset(10 * SCREENH6);
        make.width.mas_equalTo(111 * SCREENH6);
        make.height.mas_equalTo(90 * SCREENH6);
    }];
    
    [self.oneDeleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.trailing.top.mas_equalTo(self.oneButton);
        make.centerX.mas_equalTo(self.oneButton.mas_trailing).offset(-5 * SCREENH6);
        make.centerY.mas_equalTo(self.oneButton.mas_top).offset(5 * SCREENH6);
        make.width.height.mas_equalTo(30 * SCREENH6);
    }];
    
    [self.twoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.oneButton.mas_trailing).offset(5 * SCREENH6);
        make.centerY.width.height.mas_equalTo(self.oneButton);
    }];
    
    [self.twoDeleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.twoButton.mas_trailing).offset(-5 * SCREENH6);
        make.centerY.mas_equalTo(self.twoButton.mas_top).offset(5 * SCREENH6);
        make.width.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.threeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.twoButton.mas_trailing).offset(5 * SCREENH6);
        make.centerY.width.height.mas_equalTo(self.twoButton);
    }];
    
    [self.threeDeleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.threeButton.mas_trailing).offset(-5 * SCREENH6);
        make.centerY.mas_equalTo(self.threeButton.mas_top).offset(5 * SCREENH6);
        make.width.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.fourButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.oneButton.mas_bottom).offset(5 * SCREENH6);
        make.leading.width.height.mas_equalTo(self.oneButton);
    }];
    
    [self.fourDeleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.fourButton.mas_trailing).offset(-5 * SCREENH6);
        make.centerY.mas_equalTo(self.fourButton.mas_top).offset(5 * SCREENH6);
        make.width.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.fiveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.fourButton.mas_trailing).offset(5 * SCREENH6);
        make.centerY.width.height.mas_equalTo(self.fourButton);
    }];
    
    [self.fiveDeleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.fiveButton.mas_trailing).offset(-5 * SCREENH6);
        make.centerY.mas_equalTo(self.fiveButton.mas_top).offset(5 * SCREENH6);
        make.width.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.sixButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.fiveButton.mas_trailing).offset(5 * SCREENH6);
        make.centerY.width.height.mas_equalTo(self.fiveButton);
    }];
    
    [self.sixDeleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.sixButton.mas_trailing).offset(-5 * SCREENH6);
        make.centerY.mas_equalTo(self.sixButton.mas_top).offset(5 * SCREENH6);
        make.width.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.sevenButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.fourButton.mas_bottom).offset(5 * SCREENH6);
        make.leading.width.height.mas_equalTo(self.fourButton);
    }];
    
    [self.sevenDeleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.sevenButton.mas_trailing).offset(-5 * SCREENH6);
        make.centerY.mas_equalTo(self.sevenButton.mas_top).offset(5 * SCREENH6);
        make.width.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.eightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.sevenButton.mas_trailing).offset(5 * SCREENH6);
        make.centerY.width.height.mas_equalTo(self.sevenButton);;
    }];
    
    [self.eightDeleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.eightButton.mas_trailing).offset(-5 * SCREENH6);
        make.centerY.mas_equalTo(self.eightButton.mas_top).offset(5 * SCREENH6);
        make.width.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.nineButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.eightButton.mas_trailing).offset(5 * SCREENH6);
        make.centerY.width.height.mas_equalTo(self.eightButton);;
    }];
    
    [self.nineDeleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.nineButton.mas_trailing).offset(-5 * SCREENH6);
        make.centerY.mas_equalTo(self.nineButton.mas_top).offset(5 * SCREENH6);
        make.width.height.mas_equalTo(15 * SCREENH6);
    }];
}


- (UIButton *)setupButton{
    
    UIButton * button = [UIButton new];
    
    //    [button setBackgroundColor:[UIColor lightGrayColor]];
    [button setImage:[UIImage imageNamed:@"发布动态添加图片"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(iconBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    button.hidden = YES;
    
    return button;
}

- (UIButton *)setupDeleteButton{
    
    UIButton * button = [UIButton new];
    
    //    [button setBackgroundColor:[UIColor lightGrayColor]];
    [button setImage:[UIImage imageNamed:@"删除"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    button.hidden = YES;
    
    return button;
}

- (void)iconBtnClick:(UIButton *)button{
    
    //    [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil];
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickItemButton:)])
    {
        [self.delegate clickItemButton:button.tag];
    }
}


- (void)deleteBtnClick:(UIButton *)button{
    
    int tag = (int)button.tag;
    if (self.count == 1) {
        
        UIButton * btn = self.buttonArray[0];
        [btn setImage:[UIImage imageNamed:@"发布动态添加图片"] forState:UIControlStateNormal];
        UIButton * twoBtn = self.buttonArray[1];
        twoBtn.hidden = YES;
        button.hidden = YES;
    }else{
        
        for (int i = tag; i < self.count + 1; i++) {
            
            if (i == self.count) {
                
                UIButton * btn = self.buttonArray[i - 1];
                [btn setImage:[UIImage imageNamed:@"发布动态添加图片"] forState:UIControlStateNormal];
                
                for(UIButton *del in [btn subviews])
                {
                    if ([del isKindOfClass:[UIButton class]]) {
                        del.hidden = YES;
                    }
                }
                
                //                UIButton * del = self.deleteArray[i - 1];
                //                del.hidden = YES;
            }else{
                
                UIButton * btn = self.buttonArray[i - 1];
                UIButton * twoButton = self.buttonArray[i];
                
                UIImage * image =twoButton.imageView.image;
                
                [btn setImage:image forState:UIControlStateNormal];
                
                //                UIButton * del = self.deleteArray[i - 1];
                //                del.hidden = NO;
            }
            
        }
    }
    
    self.count = self.count - 1;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickDeleteButton:)])
    {
        [self.delegate clickDeleteButton:button.tag];
    }
    
}

@end
